<?php 

Class userModel extends appModel
{
	public function total($range)
	{
		$sql = " select count(*) as nums from ".$this->table('sn')." where wxid > 0 ";
	
		$total['count1'] = $this->getOne($sql);
	
		$sql = " select count(*) as nums from ".$this->table('sn');
	
		$total['count2'] = $this->getOne($sql);
		
		if($range['day1']!=""&&$range['day2']!="")
		{
			$day1 = strtotime("2015-".$range['month1']."-".$range['day1']);
			
			$day2 = strtotime("2015-".$range['month2']."-".$range['day2']);
			
			
			
			$sql = " select count(*) as nums from ".$this->table('sn')." where wxid > 0 
					and addtime >".$day1." and addtime <".$day2;
			
			$total['count3'] = $this->getOne($sql);
		}
		
		
		return $total;
	}
	
	public function lists($page=1)
	{
		$pageSize = 20;
		$listPage=array('data'=>'','pageLink'=>'');
		$where = "  1=1 and p2.nickname !='何诗茵' and p2.nickname !='007' order by p1.wxid desc ";
		$result=$this->get_page(" p1.* , p2.nickname ,p2.city ,p2.province ",$this->table('sn')." as p1 left join ".$this->table('wxusers')."  as p2 on p1.wxid=p2.id ",$where,$page,$pageSize);
		$listPage['pagelink']=$this->getpagelink();
	
		
		
		$i = 1;
		// 		$temp = array();
	
		foreach ($result as $k => $v)
		{
	
			$result[$k]['order'] = ($page-1)*$pageSize+$i;
			
			$i++;
		}
		
		$listPage['data']=$result;
	
		$sql="SELECT count(*) FROM ".$this->table('sn');
			
		$count=$this->getOne($sql);
		$listPage['count']=$count;
	
	
		return $listPage;
	}
	function userList($page=1,$sort=array())
	{
		$listPage=array('data'=>'','pageLink'=>'');
		$where=' WHERE 1=1';
		if($sort['keyword']!='')
		{
			if(hy_check($sort['keyword'],'n')!='#@_error')
			{
				$where.=" AND phone like '%".$sort['keyword']."%'";
			}
			else 
			{
				$where.=" AND realname like '%".$sort['keyword']."%'";
			}
			
		}
		$result=$this->show_page("SELECT id,realname,phone,created,last_login FROM ".$this->table('users').$where." ORDER BY id DESC",$page,'10');
		$listPage['pagelink']=$result['page'];
		$listPage['data']=$result['result'];
	
	
		return $listPage;
	}
	function getUserInfo($id='')
	{
		$sql="SELECT id,phone,realname,area,remark FROM ".$this->table('users')." WHERE id='".$id."'";
		$action=$this->getRow($sql);
		if($action['remark'])
		{
		$action['remark']=array_change_key_case(unserialize($action['remark']),CASE_UPPER);
	    
		   switch($action['remark']['SHOPAREA'])
		   {
		   	case 2:$action['remark']['shoparea']='北京';break;
		   	case 3:$action['remark']['shoparea']='安徽';break;
		   	case 4:$action['remark']['shoparea']='福建';break;
		   	case 5:$action['remark']['shoparea']='甘肃';break;
		   	case 6:$action['remark']['shoparea']='广东';break;
		   	case 7:$action['remark']['shoparea']='广西';break;
		   	case 8:$action['remark']['shoparea']='贵州';break;
		   	case 9:$action['remark']['shoparea']='海南';break;
		   	case 10:$action['remark']['shoparea']='河北';break;
		   	case 11:$action['remark']['shoparea']='河南';break;
		   	case 12:$action['remark']['shoparea']='黑龙江';break;
		   	case 13:$action['remark']['shoparea']='湖北';break;
		   	case 14:$action['remark']['shoparea']='湖南';break;
		   	case 15:$action['remark']['shoparea']='吉林';break;
		   	case 16:$action['remark']['shoparea']='江苏';break;
		   	case 17:$action['remark']['shoparea']='江西';break;
		   	case 18:$action['remark']['shoparea']='辽宁';break;
		   	case 19:$action['remark']['shoparea']='内蒙古';break;
		   	case 20:$action['remark']['shoparea']='宁夏';break;
		   	case 21:$action['remark']['shoparea']='青海';break;
		   	case 22:$action['remark']['shoparea']='山东';break;
		   	case 23:$action['remark']['shoparea']='山西';break;
		   	case 24:$action['remark']['shoparea']='陕西';break;
		   	case 25:$action['remark']['shoparea']='上海';break;
		   	case 26:$action['remark']['shoparea']='四川';break;
		   	case 27:$action['remark']['shoparea']='天津';break;
		   	case 28:$action['remark']['shoparea']='西藏';break;
		   	case 29:$action['remark']['shoparea']='新疆';break;
		   	case 30:$action['remark']['shoparea']='云南';break;
		   	case 31:$action['remark']['shoparea']='浙江';break;
		   	case 32:$action['remark']['shoparea']='重庆';break;
		   	case 33:$action['remark']['shoparea']='香港';break;
		   	case 34:$action['remark']['shoparea']='澳门';break;
		   	case 35:$action['remark']['shoparea']='台湾';break;
		   	default:$action['remark']['shoparea']='未填写';break;
		   }
		}
		
		return $action;		
	}
	function get_admin_action()
	{
		$sql="SELECT * FROM ".$this->table('admin_action');
			
		$action=$this->getAll($sql);
		foreach($action as $k=>$v)
		{
			$action[$k]['name']=Lang(strtoupper($v['module_name']));
		
		}
		
		return $action;
	}
	/**
	 * 用户角色是否已存在
	 * @param string $rolename 用户名
	 * @return boolean
	 */
	function roleEisted($rolename)
	{
		$sql="SELECT count(*) FROM ".$this->table('admin_role')." WHERE role_name = '".$rolename."'";
			
		$rolenum=$this->getOne($sql);
		if($rolenum>0)
		{
			return false;
		}
		else 
		{
			return true;
		}
	}
		/**
	 * 获取用户组
	 * 
	 * @return array
	 */

	function roleList($page=1)
	{
		$listPage=array('data'=>'','pageLink'=>'');
		
		$result=$this->get_page("id,role_name,role_des",$this->table('admin_role'),'',$page,'10');
		$listPage['pagelink']=$this->getpagelink();
		$listPage['data']=$result;
	
	
		return $listPage;
	}
	function insertRole($info)
	{
		$field='';
		$value='';
		
		foreach($info as $k=>$v)
		{
			if($v!='')
			{
				$field.="$k,";
		
					$value.="'$v',";
				
			}
		}
		$field.="created";
		$value.="'".$_SERVER['REQUEST_TIME']."'";
		
		$sql="INSERT INTO ".$this->table('admin_role')." (".$field.") VALUES(".$value.")";
		
		$res=$this->query($sql);
		
		return $res;
	}
	function del_role($id)
	{
		$result=array('error'=>0,'content'=>'');
		if($id)
		{
			$sql="DELETE FROM ".$this->table('admin_role')." WHERE  id='".$id."'";
				
			$res=$this->query($sql);
	
			if(!$res)
			{
				$result['error']=1;
				$result['content']='删除失败';
			}
			else
			{
	
	
	
			}
				
				
		}
		return $result;
	}
	function del_user($id)
	{
		$result=array('error'=>0,'content'=>'');
		if($id)
		{
			$sql="DELETE FROM ".$this->table('admin')." WHERE  id='".$id."'";
			
			$res=$this->query($sql);
		
			if(!$res)
			{
				$result['error']=1;
				$result['content']='删除失败';
			}
			else 
			{
				
				  
				
			}
			
			
		}
		return $result;
	}
	function updateRole($info=array(),$id)
	{
		$where='';
	
		$res=null;
		if($id)
		{
			
				foreach($info as $k=>$v)
				{
					if($v!='')
					{
						
							$where.=" $k='$v',";
						
					}
				}
				$where.="modified=".$_SERVER['REQUEST_TIME'];
					
				$sql="UPDATE ".$this->table('admin_role')." SET ".$where." WHERE id='".$id."'";
				$res=$this->query($sql);
			
		}
		return $res;
	}
	function get_role_list()
	{
		$sql="SELECT id,role_name FROM ".$this->table('admin_role');
		$role=$this->getAll($sql);
		return $role;
	}
	function getRoleInfo($id='')
	{
		$sql="SELECT role_name,role_des,role_action FROM ".$this->table('admin_role')." WHERE id = '".$id."'";
		$role=$this->getRow($sql);
		return $role;
	}
	function getAdminInfo($id='')
	{
		
		if(session('admin_id'))
		{
			if($id)
			{
				
					$sql="SELECT qq,email,username,group_id FROM ".$this->table('admin')." WHERE id = '".$id."'";
						
					$result=$this->getRow($sql);
					if($result)
					{
						return $result;
					}
				
			}
			else 
			{
			$sql="SELECT qq,email,username FROM ".$this->table('admin')." WHERE id = '".session('admin_id')."'";
			
			$result=$this->getRow($sql);
			  if($result)
			  {
				return $result;
			  }
			   else 
			  {
				return $result=array();
			  }
			}
		}
		else 
		{
			return $result=array();
		}
	}
	/**
	 * 插入用户资料时验证信息是否已存在
	 * @param string $username 用户名
	 * @param string $email 电子邮箱
	 * @return string
	 */
	function isExist($arr)
	{
		$result=array('error'=>0,'content'=>'');
		
		$msg='';
		foreach($arr as $k=>$v)
		{
			if($k=='email'||$k=='username')
			{
				
			   $sql="SELECT count(*) FROM ".$this->table('admin')." WHERE $k='$v'";
			   $query=$this->getOne($sql);
			  
			   if($query>0)
			  {
			  	$result['error']=$result['error']+1;
				$msg .=sprintf(Lang('this_has_exist'),$v);
			  }
			}
			
		}
		
		$result['content']=$msg;
		return $result;
	}
	/**
	 * 验证更新用户资料时的准确性
	 * @param string $username 用户名
	 * @param string $email 电子邮箱
	 * @return boolean
	 */
	function checkInfo($username,$email,$id='')
	{   
		if($id=='')
		{
			$id=session('admin_id');
		}
		//验证email
		if (!preg_match("/^[a-z]([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?$/i ",$email))
		{
			$this->showMsg(Lang('email_format_error'));
			return false;
		}
		$sql="SELECT email FROM ".$this->table('admin')." WHERE id='".$id."'";
		$oldemail=$this->getOne($sql);
		$email_exist=0;
		if($oldemail!=$email)
		{
			$sql="SELECT * FROM ".$this->table('admin')." WHERE email='$email'";
			$email_exist=$this->getOne($sql);
			
		}
		
		if($email_exist>0)
		{
			$this->showMsg(Lang('email_has_exist'));
			return false;
		}
		
		//验证用户名是否相同
		$sql="SELECT username FROM ".$this->table('admin')." WHERE id='".$id."'";
		$oldname=$this->getOne($sql);
		$name_exist=0;
		
		if($oldname!=$username)
		{
			$sql="SELECT * FROM ".$this->table('admin')." WHERE username='$username'";
			$name_exist=$this->getOne($sql);
			
		}
		
		if($name_exist>0)
		{
			$this->showMsg(Lang('username_has_exist'));
			return false;
		}
		return true;
		
	}
     public function is_repeat($data,$name)
	{
		$sql="SELECT 1 FROM ".$this->table('users')." WHERE ".$name."='".$data."'";
		$res=$this->getOne($sql);
	
		return $res;
	}
	function importData($path)
	{
		$data=new ExcelReader();
		$data->setOutputEncoding('UTF-8');
		@ini_set("max_execution_time", "0");
		set_time_limit(0);
		@ini_set('memory_limit',          '1024M');
		
		$data->read(ROOT_PATH.$path);
		
		
		$origin_pwd=md5('0000');
		$sql = "INSERT INTO ".$this->table('sn')."(sn) VALUES";
		for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
	
			$sql.="('".
					$data->sheets[0]['cells'][$i][1]."')";
			
				$this->query($sql);
				echo '已导入第'.$i.'条数据<br>';
				$sql = "INSERT INTO ".$this->table('sn')."(sn) VALUES";
			
			
			
		}
		return true;
	}
	
	/**
	 * 获取所有用户
	 * @param 
	 * @return array
	 */
	function adminList($page=1)
	{
		$listPage=array('data'=>'','pageLink'=>'');
	
		$result=$this->show_page("SELECT a.id,a.username,(SELECT b.role_name FROM ".$this->table('admin_role')." as b WHERE a.group_id=b.id) as group_name FROM ".$this->table('admin')." as a",$page,'10');
		$listPage['pagelink']=$result['page'];
	    $listPage['data']=$result['result'];
		
		
		return $listPage;
	}
	function search_admin($page=1,$keyword)
	{
		$listPage=array('data'=>'','pageLink'=>'');
	
		if($keyword)
		{
			$sql="SELECT a.id,a.username,(SELECT b.role_name FROM ".$this->table('admin_role')." as b WHERE a.group_id=b.id) as group_name FROM ".$this->table('admin')." as a WHERE username like '%".$keyword."%'";
	
			$result=$this->show_page($sql,$page,'10');
			$listPage['pagelink']=$result['page'];
			$listPage['data']=$result['result'];
	
		}
	
		return $listPage;
	}
	function search_user($page=1,$keyword)
	{
		$listPage=array('data'=>'','pageLink'=>'');
		
		if($keyword)
		{
		$sql="SELECT id,integral,phone FROM ".$this->table('users')."  WHERE phone like '%".$keyword."%'";
		
		$result=$this->show_page($sql,$page,'10');
		$listPage['pagelink']=$result['page'];
	    $listPage['data']=$result['result'];
		
		}
	
		return $listPage;
	}
	
	
	/**
	 * 添加新用户
	 * @param array $info
	 * @return boolean
	 */
	function insertInfo($info=array())
	{
		$field='';
		$value='';
		$update_pwd=false;
		foreach($info as $k=>$v)
		{
			if($v!='')
			{
				$field.="$k,";
				if($k=='password')
				{
					$value.="'".md5($v)."',";
					
				}
				else
				{
					
					$value.="'$v',";
				}
			}
		}
		$field.="created";
		$value.="'".$_SERVER['REQUEST_TIME']."'";
	
		$sql="INSERT INTO ".$this->table('admin')." (".$field.") VALUES(".$value.")";
		
		$res=$this->query($sql);
		
		return $res;
		
	}
	
	/**
	 * 超级管理员修改用户资料
	 * @param array $info
	 * *@param int $id
	 * @return boolean
	 */
	function updateInfo($info=array(),$id)
	{
		$where='';
		$update_pwd=false;
		$res=null;
		if($id)
		{
				$sql="SELECT group_id FROM ".$this->table('admin')." WHERE id = '".session('admin_id')."'";
					
				$group_id=$this->getOne($sql);
				if($group_id==1)//超级管理员才有权限
				{
					foreach($info as $k=>$v)
					{
						if($v!='')
						{
							if($k=='password')
							{
								$where.=" $k='".md5($v)."',";
								$update_pwd=true;
							}
							else
							{
								$where.=" $k='$v',";
							}
						}
					}
					$where.="modified=".$_SERVER['REQUEST_TIME'];
					
					$sql="UPDATE ".$this->table('admin')." SET ".$where." WHERE id='".$id."'";
					if($res=$this->query($sql))
					{
						if($id == session('admin_id')&&$update_pwd)
						{
							cookie('QNHCMS[admin_id]',   '');
							cookie('QNHCMS[admin_pass]', '');
					
					
					
							session('[destroy]');
						}
					}
				}
		}
		
		return $res;
		//invalid_operation
	}
	/**
	 * 保存用户资料
	 * @param array $info
	 * @return boolean
	 */
	function saveInfo($info=array())
	{
		$where='';
		$update_pwd=false;
		foreach($info as $k=>$v)
		{
			if($v!='')
			{
				if($k=='password')
				{
					$where.=" $k='".md5($v)."',";
					$update_pwd=true;
				}
				else 
				{
					$where.=" $k='$v',";
				}
			}
		}
		$where.="modified=".$_SERVER['REQUEST_TIME'];
	
		$result==array('error'=>0,'content'=>'');
		$sql="UPDATE ".$this->table('admin')." SET ".$where." WHERE id='".session('admin_id')."'";
		if($res=$this->query($sql))
		{
			if($update_pwd)
			{
				$result['error']=-1;
			}
			
		}
		else 
		{
			$result['error']=1;
			$result['content']=Lang('SQL_ERROR');
		}
		return $result;
		//invalid_operation
	}
}